package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.manager.bank.ebank.TBankB2bManager;
import com.gopay.common.Result;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.tbank.ebank.pay.TBankMessage;
import com.gopay.common.domain.bank.tbank.ebank.query.req.TBankOrderReq;
import com.gopay.common.domain.bank.tbank.ebank.query.res.TBankOrderRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

/**
 * 测试银行B2B实现
 * 
 * @TbankB2bBankServiceImpl.java
 * @author WangHongFei
 */
public class TBankB2bBankServiceImpl implements BankInteractRemoteService {

    @Resource(name = "tbankB2bManager")
    private TBankB2bManager tbankB2bManager;

    /**
     * 支付类请求
     * 
     * @param BaseMessage
     * @return Result
     */
    public Result pay(BaseMessage base) {
        Result result = new Result(false);
        base.setBankOrderId(base.getOrderId());
        TBankMessage messageB2B = new TBankMessage();
        BeanUtils.copyProperties(base, messageB2B);
        try {
        	messageB2B = (TBankMessage)tbankB2bManager.sign(messageB2B);
	        result.setDefaultModel(messageB2B);
	        result.setSuccess(true);	        
		}catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
    		result.setResultCode(e.getErrCode());
		}catch(Exception ex){
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
        return result;
    }

    /**
     * 银行返回类请求
     * 
     * @param BaseMessage
     * @return Result
     */
    @Override
    public Result bankBack(BaseMessage base) {
    	Result result = new Result(false);
    	try{
            TBankMessage messageB2B = new TBankMessage();
            BeanUtils.copyProperties(base, messageB2B);
        	boolean flag = (Boolean)tbankB2bManager.verify(messageB2B);
            result.setDefaultModel(flag);
            result.setSuccess(true);
            if (result != null && result.isSuccess() && (Boolean) result.getDefaultModel()) {
            	Map<String, String> param = new HashMap<String, String>();
            	if(messageB2B.getRetCd()!=null && messageB2B.getRetCd().equals("S0100")){
                    // 如果该订单成功通知后台地址入账                    
                    param.put("orderId", messageB2B.getOrderId());
                    param.put("amt", messageB2B.getAmount().toString());
                    param.put("cardType", "Unknown");
                    param.put("notifyNum", "1");
                    param.put("payStatus", "00");
                    param.put("retCode", "00");
                    param.put("retMsg", messageB2B.getRetMsg());
                    param.put("signData", messageB2B.getSignMessage());
                    param.put("isFront", base.isFront()+"");
                    result.setModel("isNotifyCashier", param);
                    result.setOrderStatus(true);
            	}else{
                    // 通知后台地址该订单失败
                    param.put("orderId", messageB2B.getOrderId());
                    param.put("amt", messageB2B.getAmount().toString());
                    param.put("cardType", "Unknown");
                    param.put("notifyNum", "1");
                    param.put("payStatus", "01");
                    param.put("retCode", "01");
                    param.put("retMsg", messageB2B.getRetMsg());
                    param.put("signData", messageB2B.getSignMessage());
                    param.put("isFront", base.isFront()+"");
                    result.setModel("isNotifyCashier", param);
                    result.setOrderStatus(false);
            	}
            }
    	}catch(GopayException e){
    		BankLogUtils.printServiceImplExceptionLog(e);
    		result.setResultCode(e.getErrCode());
    	}catch(Exception ex){
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}        
        return result;
    }

    /**
     * 银行查询类请求
     * 
     * @param BaseMessage
     * @return Result
     */
    @Override
    public Result query(BaseMessage base) {
        Result result = new Result(false);
        TBankOrderReq orderReq = new TBankOrderReq();
        BeanUtils.copyProperties(base, orderReq);
        try {
        	TBankOrderRes orderRes = (TBankOrderRes)tbankB2bManager.query(orderReq);
	        result.setDefaultModel(orderRes);
	        result.setSuccess(true);	        
		}catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
    		result.setResultCode(e.getErrCode());
		}catch(Exception ex){
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
        return result;
    }

    /**
     * 银行退款类请求
     * 
     * @param BaseMessage
     * @return Result
     */
	@Override
	public Result refund(BaseMessage base) {
		return null;
	}

    /**
     * 银行日终类请求
     * 
     * @param BaseMessage
     * @return Result
     */
	@Override
	public Result dayend(BaseMessage base) {
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
